<?php

class CategoryRecord extends TActiveRecord
{
	const TABLE='tbl_category';

	public $ID;
	public $ParentID;
	public $Name;
	public $Alias;
	public $Description;
	public $ImagePath;
	public $ThumbnailPath;
	public $IsPublished;
	public $Ordering;
	public $IsFrontPage;
	public $CreateBy;
	public $CreateDate;
	public $ModifyBy;
	public $ModifyDate;	
	
	public static $COLUMN_MAPPING=array
	(
		'cat_id'=>'ID',
		'parent_id'=>'ParentID',
		'cat_name'=>'Name',
		'cat_alias'=>'Alias',
		'cat_desc'=>'Description',
		'cat_image'=>'ImagePath',
		'cat_thumb'=>'ThumbnailPath',
		'cat_publish'=>'IsPublished',
		'cat_order'=>'Ordering',
		'cat_frontpage'=>'IsFrontPage',
		'c_by'=>'CreateBy',
		'c_date'=>'CreateDate',
		'm_by'=>'ModifyBy',
		'm_date'=>'ModifyDate'
	);
	
	public static $RELATIONS=array
	(
		'Parent'=>array(self::BELONGS_TO, 'CategoryRecord', 'parent_id'),
		'Childs'=>array(self::HAS_MANY, 'CategoryRecord', 'parent_id'),
		'Products'=>array(self::MANY_TO_MANY, 'ProductRecord', 'tbl_product_cat')
	);

	public static function finder($className=__CLASS__)
	{
		return parent::finder($className);
	}
	
	public function save()
	{
		if ($this->ID <= 0)
		{
			$criteria = new TActiveRecordCriteria;
			$criteria->Condition = "cat_id <> 0 and parent_id = :id";
			$criteria->Parameters[":id"] = $this->ParentID;
			$this->Ordering = self::finder()->count($criteria)+1;
			$this->CreateDate = time();
		}
		$this->Alias = String::removeAccents(strlen($this->Alias) > 0 ? $this->Alias : $this->Name);
		$this->ModifyDate = time();
		parent::save();
	}
	
	public function getAllParent($publishedOnly=false)
	{
		$criteria = new TActiveRecordCriteria;
		$criteria->Condition = "cat_id > 0 and parent_id = 0";
		if ($publishedOnly) $criteria->Condition .= " and cat_publish = 1";
		$criteria->OrdersBy["cat_name"] = "asc";
		return self::finder()->findAll($criteria);
	}
	
	public function getTotalItemCount($id=0)
	{
		$criteria = new TActiveRecordCriteria;
		$criteria->Condition = "cat_id > 0 ";
		if ($id>0)
		{
			$cat = self::finder()->withParent()->findByPk($id);
			if ($cat) $criteria->Condition .= " and parent_id = '".$cat->Parent->ID."'";
		}
		return self::finder()->count($criteria);
	}
	
	public function getCategoryTree($publishedOnly=false)
	{
		$this->_allRecords = array();
		foreach($this->getAllParent($publishedOnly) as $parent)
		{
			$this->_allRecords[] = $parent;
			$this->getChildByParent($parent->ID,"--- ",$publishedOnly);
		}
		return $this->_allRecords;
	}
	
	private $_allRecords=array();
	
	protected function getChildByParent($parentID,$indent="--- ",$publishedOnly=false)
	{
		$criteria = new TActiveRecordCriteria;
		$criteria->Condition = "parent_id = :parent";
		if ($publishedOnly) $criteria->Condition .= " and cat_publish = 1";
		$criteria->Parameters[":parent"] = $parentID;
		$criteria->OrdersBy["cat_name"] = "asc";
		$childs = self::finder()->findAll($criteria);
		if (count($childs)<=0) return;
		foreach($childs as $child)
		{
			$child->Name = $indent.$child->Name;
			$this->_allRecords[] = $child;
			$this->getChildByParent($child->ID);
		}
	}

}
?>